# -*- coding:utf-8 -*-

import redis
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from flask_session import Session
from config import config

# 创建 SQLAlchemy的 db 对象
db = SQLAlchemy()


# 工厂方法：根据传入的参数，创建出指定参数所对应的对象
def create_app(config_name):
    app = Flask(__name__)
    # 加载配置
    app.config.from_object(config[config_name])
    # 数据库
    # db = SQLAlchemy(app)
    # 关联app
    db.init_app(app)
    # redis
    redis_store = redis.StrictRedis(host=config[config_name].REDIS_HOST, port=config[config_name].REDIS_PORT)
    # 开启CSRF保护，只做校验
    CSRFProtect(app)
    # 指定Session保存的位置
    Session(app)

    return app
